package com.qf.service.impl;

import com.qf.service.MailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * @author lenovo
 * @version V1.0
 * @Date 2021/11/26 16:17
 */
@Service
@Slf4j
public class EmailServiceImpl implements MailService {

    //前缀
    private final String PRE = "<h1 style=’color:red;’>";

    //后缀
    private final String SUF = "</h1>";


    @Value("${spring.mail.to}")
    private String mailReciver;

    @Value("${spring.mail.username}")
    private String mailSender;

    @Autowired
    private JavaMailSender javaMailSender;

    /**
     * 发送邮件方法
     */
    @Override
    public void send(String msgContent) {

        try {
            //创建邮件
            MimeMessage message = javaMailSender.createMimeMessage();
            MimeMessageHelper messageHelper = new MimeMessageHelper(message,true);
            messageHelper.setTo(mailReciver);
            messageHelper.setFrom(mailSender);
            //TODO 这个设置html不生效
            messageHelper.setText(msgContent,true);
            messageHelper.setSubject("系统预警！！！");
            //发送邮件
            javaMailSender.send(message);
        } catch (MessagingException e) {
            log.info("【检测模块 - 发送邮件】邮件发送失败！！！！");
            e.printStackTrace();

        }

        log.info("【检测模块 - 发送邮件】邮件发送成功！！！！");


    }
}
